<?php

$container_bl=join('&nbsp;', $form->_button_container['BL']);
$container_br=join('&nbsp;', $form->_button_container['BR']);
$container_tr=join('&nbsp;', $form->_button_container['TR']);

if ($form->_status=='delete' || $form->_action=='delete' || $form->_status=='unknow_record'):
	echo $form->output;
else:
$transac=$form->transac->value;
$usuario=$this->datasis->dameval("SELECT usuario FROM gser WHERE transac='$transac'");

$campos=$form->template_details('gitser');
$scampos  ='<tr id="tr_gitser_<#i#>">';
$scampos .='<td class="littletablerow" align="left" >'.$campos['cuenta']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['desca']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['ruc']['field'].'</td>';
$scampos .='<td class="littletablerow" align="left" >'.$campos['tipo']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['vexento']['field']. '</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['importe']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['exento']['field'].'</td>';
$scampos .='<td class="littletablerow" align="right">'.$campos['creditos']['field'];

$scampos .= '<td class="littletablerow"><a href=# onclick="del_gitser(<#i#>);return false;">'.img("images/delete.jpg").'</a></td></tr>';
$campos=$form->js_escape($scampos);

if(isset($form->error_string)) echo '<div class="alert">'.$form->error_string.'</div>';

echo $form_scripts;
echo $form_begin;

$link=base_url()."common/numgser/";
$link2=base_url()."common/getcomp/";

if($form->_status!='show'){ ?>

<script language="javascript" type="text/javascript">
var gitser_cont=<?php echo $form->max_rel_count['gitser']; ?>;

$(function(){
	$(".inputnum").numeric(".");
	vpr();
});
function vpr(){
	
	pr0    = Number($("#porcrete").val());
	//pr1    = Number($("#pr1").val());
	//pr2    = Number($("#pr2").val());
	//pr3    = Number($("#pr3").val());
	
	totals = Number($("#totals").val());
	iva    = Number($("#iva").val());
	credito= Number($("#credito").val());
	
	totalg =totals+iva;
	
	rete=$("#rete").val(roundNumber((totalg*pr0/100),2));
	
	//rete1=$("#rete1").val(roundNumber((totalg*pr1/100),2));
	//rete2=$("#rete2").val(roundNumber((totalg*pr2/100),2));
	//rete3=$("#rete3").val(roundNumber((totalg*pr3/100),2));
	//
	//rete1     = Number($("#rete1").val());
	//rete2     = Number($("#rete2").val());
	//rete3     = Number($("#rete3").val());
	
   
	rete   = Number($("#rete").val());
	
    $("#totalg").val(roundNumber((totalg-credito),2));

}
function tcomp(){
	
	comp      = $("#comp").val();
	
	$.ajax({
		url: "<?=$link2?>"+comp,
		success: function(msg){
			if(msg){
				//$("#seriei").val(msg);
				$("#seriei").html(msg)
			} else {
				alert("No es posible generar una lista");
			}
		}
	});

}	
function numgser(){
	
	seriei    = $("#seriei").val();
	comp      = $("#comp").val();
	
	if(seriei=='010'){
		
		$("#numerond").show();
		$("#seriend").show();
		
	}else{
		
		$("#numerond").hide();
		$("#seriend").hide();
		
	}
	
	$.ajax({
		url: "<?=$link?>"+seriei+'/'+comp,
		success: function(msg){
			if(msg){
				$("#numeroi").val(msg);
			} else {
				alert("No es posible generar un numero");
			}
		}
	});
}
function importe(id){
	
	var ind     = id.toString();
	var vexento = $("#vexento_"+ind).val();
	var vcreditos = $("#tipo_"+ind).val();
		
	importe2= Number($("#importe_"+ind).val());
	
	//$("#creditos_"+ind).attr('disabled','disabled');
	//$("#exento_"+ind).attr('disabled','disabled');
	
	if(vcreditos=='D'){

		if(vexento=='S'){
			$("#exento_"+ind).val(importe2);	

		}else{
			$("#exento_"+ind).val(0);
		}
		
		$("#creditos_"+ind).val(0);	
	
	}else{
	
		$("#creditos_"+ind).val(importe2);
	
	}
	
	//var tipo = $("#tipo_"+ind).val();
	
	totalizar();
}
function totalizar(){
	
	//var iva    =0;
	var totalg =0;
	var totals =0;
	var texento =0;
	var tcreditos =0;
	var ivap   =18;
	var importe=0;
	var rete=0;
	
	var arr=$('input[name^="importe_"]');
	jQuery.each(arr, function() {
		nom=this.name;
		pos=this.name.lastIndexOf('_');
		if(pos>0){
			importe = Number(this.value);	
			totals  = (totals+importe);
		}
	});
	
	var arr=$('input[name^="exento_"]');
	jQuery.each(arr, function() {
		nom=this.name;
		pos=this.name.lastIndexOf('_');
		if(pos>0){
			exento = Number(this.value);	
			texento  = (texento+exento);
		}
	});
	
	var arr=$('input[name^="creditos_"]');
	jQuery.each(arr, function() {
		nom=this.name;
		pos=this.name.lastIndexOf('_');
		if(pos>0){
			creditos  = Number(this.value);	
			tcreditos = (tcreditos+creditos);
		}
	});
	
	$("#mexento").val(roundNumber(texento,2));
	
	sexento    = $("#mexento").val();
	subtotal=totals-tcreditos;
		
	tot=subtotal-sexento;
	iva     = (tot*ivap/100);
	
	$("#iva").val(roundNumber(iva,2));
	$("#totals").val(roundNumber(subtotal,2));
	$("#credito").val(roundNumber(tcreditos,2));
	
	vpr();
}
function add_gitser(){
	var htm = <?php echo $campos; ?>;
	can = gitser_cont.toString();
	con = (gitser_cont+1).toString();
	htm = htm.replace(/<#i#>/g,can);
	htm = htm.replace(/<#o#>/g,con);
	$("#__INPL__").after(htm);
	$("#cana_"+can).numeric(".");
	$('#cuenta_'+can).focus();
	gitser_cont=gitser_cont+1;
}
function del_gitser(id){
	id = id.toString();
	$('#tr_gitser_'+id).remove();
	totalizar();
}
</script>
<?php } ?>
<table align='center' width="90%" style='background: #EEEEEE'>
	<tr>
		<?php if ($form->_status=='show') { ?>
		<td>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/verhtml/GSER/<?php echo $form->transac->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/html_icon.gif'></a>
		<a href="#" onClick="window.open('<?php echo base_url() ?>formatos/ver/GSER/<?php echo $form->transac->value ?>', '_blank', 'width=800, height=600, scrollbars=Yes, status=Yes, resizable=Yes, screenx='+((screen.availWidth/2)-400)+',screeny='+((screen.availHeight/2)-300)+'');" heigth="600" >
		<img src='<?php echo base_url() ?>images/pdf_logo.gif'></a> Comprobante 
		</td>
	<?php } ?>
	  <td colspan="3" align="right"><?php echo $container_tr?><?php echo $container_br?></td>
  </tr>
</table>
<table align='center'>
	<tr>
		<td>
         <table align='center' width="100%">
	<tr>
		<td>
		<table width='100%'><tr><td>
		<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
		<legend class="titulofieldset" style='color: #2E4B77;'>Informaci&ograve;n de la FACTURA</legend>
			<table style="margin: 0; width: 100%;">
			<tr>
				<td class="littletableheader"><?php echo $form->seriei->label;    ?></td>
				<td class="littletablerow" colspan=2 ><?php echo $form->comp->output;   ?> <?php echo $form->seriei->output;   ?>-<?php echo $form->numeroi->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->seriend->label;    ?></td>
				<td class="littletablerow" colspan=2 ><?php echo $form->seriend->output;   ?>-<?php echo $form->numerond->output;?>Solo para Notas de Debito</td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->numero->label;    ?>*</td>
				<td class="littletablerow" colspan=2><strong><?php echo $form->seriep->output;   ?>-<?php echo $form->numero->output;   ?></strong></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->fecha->label;    ?></td>
				<td colspan="2" class="littletablerow">   <?php echo $form->fecha->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->vence->label;    ?></td>
				<td colspan="2" class="littletablerow">   <?php echo $form->vence->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->recepcion->label;    ?></td>
				<td colspan="2" class="littletablerow">   <?php echo $form->recepcion->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->ccosto->label;    ?></td>
				<td colspan="2" class="littletablerow">   <?php echo $form->ccosto->output;   ?></td>
			</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->cuentai->label;    ?></td>
				<td colspan="2" class="littletablerow">   <?php echo $form->cuentai->output;   ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->moneda->label     ?></td>
			  <td colspan="2" class="littletablerow"><?php echo $form->moneda->output    ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->cambio->label     ?></td>
			  <td colspan="2" class="littletablerow"><?php echo $form->cambio->output    ?></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->formap->label     ?></td>
			  <td colspan="2" class="littletablerow"><?php echo $form->formap->output    ?></td>
			</tr>
			</table>
			</fieldset>
		</td><td width="45%">
			<fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
			<legend  class="titulofieldset" style='color: #2E4B77;'>Identificaci&oacute;n del Proveedor </legend>
			<table  style="margin: 0; width: 100%;">
			<tr>
				<td width="33%" class="littletableheader"><?php echo $form->proveed->label;  ?></td>
				<td class="littletablerow"><?php echo $form->proveed->output; ?></td>
				</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->nombre->label;  ?></td>
				<td class="littletablerow"><?php echo $form->nombre->output;  ?></span></td>
			</tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->rif->label; ?></td>
			  <td class="littletablerow"><?php echo $form->rif->output;   ?></td>
			  </tr>
			<tr>
			  <td class="littletableheader"><?php echo $form->cajachica->label; ?></td>
			  <td class="littletablerow"><?php echo $form->cajachica->output;   ?></td>
			  </tr>
			</table>
			</fieldset>
            <?php if ($form->_status=='show') { ?>
            <fieldset style='border: 2px outset #9AC8DA;background: #FFFFFF;'>
			<legend  class="titulofieldset" style='color: #2E4B77;'>Identificaci&oacute;n del Usuario </legend>
			<table  style="margin: 0; width: 100%;">
			<tr>
				<td width="33%" class="littletableheader">Usuario</td>
				<td class="littletablerow"><?php echo $usuario; ?></td>
				</tr>
			<tr>
				<td class="littletableheader"><?php echo $form->estampa->label;  ?></td>
				<td class="littletablerow"><?php echo $form->estampa->output;  ?></span></td>
			</tr>
			</table>
			</fieldset>
            <? }?>
		</td><tr></table>
		<br>
		</td>
	</tr>
	<tr>
		<td>
		<div style='overflow:auto;border: 1px solid #9AC8DA;background: #FAFAFA;height:200px'>
		<table width='95%' border='0' align="center">
			<tr id='__INPL__' class="littletablerow">
				<td bgcolor='#527D9E'>Cuenta</td>
				<td bgcolor='#527D9E'>Descripci&oacute;n</td>
				<td bgcolor='#527D9E'>RUC</td>
				<td bgcolor='#527D9E'>Tipo</td>
				<td bgcolor='#527D9E'>Afecto</td>
				<td bgcolor='#527D9E'>Monto sin IGV</td>
				<td bgcolor='#527D9E'>Monto Inafecto</td>
				<?php if ($form->_status!='show') { ?>
				<td bgcolor='#527D9E'>Creditos</td>
				<?}?>
				<?php if($form->_status!='show') {?>
					<td class="littletableheader">&nbsp;</td>
				<?php } ?>
			</tr>

			<?php for($i=0;$i<$form->max_rel_count['gitser'];$i++) {
				$it_cuenta  = "cuenta_$i";
				$it_desca   = "desca_$i";
				$it_ruc     = "ruc_$i";
				$it_tipo    = "tipo_$i";
				$it_cana    = "cana_$i";
				$it_vexento = "vexento_$i";
				$it_preca   = "preca_$i";
				$it_importe = "importe_$i";
				$it_exento  = "exento_$i";
				$it_creditos= "creditos_$i";

			?>

			<tr id='tr_gitser_<?php echo $i; ?>'>
				<td class="littletablerow" align="left"><?php echo $form->$it_cuenta->output; ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_desca->output;  ?></td>
				<td class="littletablerow" align="left"><?php echo $form->$it_ruc->output;  ?></td>
				<td class="littletablerow" align="center"><?php echo $form->$it_tipo->output;  ?></td>
				<td class="littletablerow" align="center"><?php echo $form->$it_vexento->output;   ?></td>
				<td class="littletablerow" align="right"><?php echo $form->$it_importe->output;?></td>
				<td class="littletablerow" align="right"><?php echo $form->$it_exento->output;?></td>
				<?php if ($form->_status!='show') { ?>
				<td class="littletablerow" align="left"><?php echo $form->$it_creditos->output;?></td>
				<?php } ?>
				<?php if($form->_status!='show') {?>
				<td class="littletablerow">
					<a href='#' onclick='del_gitser(<?=$i ?>);return false;'><?php echo img("images/delete.jpg"); ?></a>				</td>
				<?php } ?>
			</tr>
			<?php } ?>

			<tr id='__UTPL__'>
				<td id='cueca'></td>
			</tr>
		</table>
		</div>
		<?php echo $container_bl ?>
		</td>
	</tr>
	<tr>
		<td>
		<fieldset style='border: 2px outset #9AC8DA;background: #EFEFEF;'>
		<legend  class="titulofieldset" style='color: #2E4B77;' align="right">-- Totales --</legend>
		<table width='100%'>
			<tr>
				<td class="littletableheader" width='100'><?php echo $form->condi1->label;    ?></td>
				<td class="littletablerow"    width='643' ><?php echo $form->condi1->output;   ?></td>
				<td width="200" align='right' class="littletableheader"><?php echo $form->totals->label;  ?></td>
				<td width="138" align='right' class="littletablerow"><?php echo $form->totals->output ?></td>
			</tr>	
			<tr><td class="littletableheader">&nbsp;</td>
				<td class="littletablerow"   >&nbsp;</td>
				<td align='right' class="littletableheader"><?php echo $form->ivat->label;    ?> <?php echo nformat($this->datasis->dameval("SELECT tasa FROM civa ORDER BY fecha DESC"),0); ?> %</td>
				<td class="littletablerow" align='right' ><?php echo $form->ivat->output; ?></td>
			</tr>
			<tr>
				<td>&nbsp;</td>
				<td class="littletablerow">&nbsp;</td>
				<td align='right' class="littletableheader"><?php echo $form->totalg->label;  ?></td>
				<td class="littletablerow" align='right' ><?php echo $form->totalg->output; ?></td>
			</tr>
		</table>
		</fieldset>
		</td>
	</tr>
	<tr>
		<td>
		<fieldset style='border: 2px outset #9AC8DA;background: #EFEFEF;'>
		<legend  class="titulofieldset" style='color: #2E4B77;' align="right">Detracciones y Retenciones</legend>
		<table width='100%'>
			<tr>
				<td colspan='2' align='right' class="littletablerow">Detracciones</td>
				<td colspan='2' align='right' class="littletablerow">Retenciones</td>
			</tr>
			<tr>
				<td width='50%' align='right' class="littletableheader"><?php echo $form->rete->label;  ?></td>
				<td align='right' class="littletablerow"><?php echo $form->porcrete->output; ?>-<?php echo $form->rete->output; ?></td>
				<td width='20%' align='right' class="littletableheader"><?php echo $form->credito->label;  ?></td>
				<td align='right' class="littletablerow"><?php echo $form->credito->output; ?></td>
			</tr>
			<tr>
				<td align='right' class="littletableheader"><?php echo $form->codrete->label;  ?></td>
				<td align='right' class="littletablerow"><?php echo $form->codrete->output; ?></td>
				<td align='right' class="littletableheader"><?php echo $form->mexento->label;  ?></td>
				<td align='right' class="littletablerow"><?php echo $form->mexento->output; ?></td>

			</tr>
			<tr>
				<td align='right' class="littletableheader"><?php echo $form->fvrete->label;  ?></td>
				<td align='right' class="littletablerow"><?php echo $form->fvrete->output; ?></td>
			<?php if($form->_status=='show') {?>	
				<td align='right' class="littletableheader"><?php echo $form->totneto->label;  ?></td>
				<td align='right' class="littletablerow"><strong><?php echo $form->totneto->output; ?></strong></td>
			<?php } ?>
			</tr>		 
		</table>
		</fieldset>
		</td>
	</tr>
</table>
<?php echo $form_end?>
<?php echo $container_br ?>
	  <td>
	<tr>
<table>
<?php endif; ?>
